package com.yuanxiao.controller;

import com.yuanxiao.dao.TokenDao;
import com.yuanxiao.dao.UserDao;
import com.yuanxiao.feign.CodeServiceClient;
import com.yuanxiao.pojo.AuthToken;
import com.yuanxiao.pojo.User;
import com.yuanxiao.service.CodeService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.domain.Example;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Optional;
import java.util.UUID;

@RestController
@RequestMapping("/api/user")
@RefreshScope
public class UserController {
   /* @Autowired
    private CodeServiceClient codeServiceClient;
*/
    @Autowired
    private TokenDao tokenDao;

    @Autowired
    private UserDao userDao;

    @Reference
    private CodeService codeService;

    @GetMapping("/register/{email}/{password}/{code}")
    public Boolean register(@PathVariable String email,
                            @PathVariable String password,
                            @PathVariable String code,
                            HttpServletResponse response){
        if(StringUtils.isEmpty(email)){
            return false;
        }
        if(StringUtils.isEmpty(password)){
            return false;
        }
        if(StringUtils.isEmpty(code)){
            return false;
        }
        Boolean validateCode = codeService.validateCode(email, code);
        boolean flag= false;
        if(validateCode){
            if(!isRegistered(email)){
                userDao.save(new User().setEmail(email).setPassword(password));
                //携带一个token
                //1生成一个token
                Cookie cookie = pullToken(email);
                response.addCookie(cookie);
                flag=true;
            }
        }

    return flag;
    }

    private Cookie pullToken(String email) {
        String s = UUID.randomUUID().toString();
        tokenDao.save(new AuthToken().setEmail(email).setToken(s));
        Cookie cookie = new Cookie("token",s);
        cookie.setPath("/");
        cookie.setMaxAge(60*60*24);//保留一天
        return cookie;
    }

    @GetMapping("/isRegistered/{email}")
    public boolean isRegistered(@PathVariable String email) {

        if (StringUtils.isEmpty(email)) {
            return false;
        }
        return userDao.exists(Example.of(new User().setEmail(email)));
    }

    @GetMapping ("/login/{email}/{password}")
    public Boolean login(@PathVariable String email, @PathVariable String password,
                         HttpServletResponse response) {

        if (StringUtils.isEmpty(email)) {
            return false;
        }
        if (StringUtils.isEmpty(password)) {
            return false;
        }

        if (checkTheUserPassword(email,password)) {
            Cookie cookie = pullToken(email);
            response.addCookie(cookie);
            return true;
        }
        return false;
    }

    private boolean checkTheUserPassword(String email, String password) {
        return userDao.exists(Example.of(new User().setEmail(email).setPassword(password)));
    }

    @GetMapping("/info/{token}")
    public String  getEmail(@PathVariable String  token){
        AuthToken byToken = tokenDao.findByToken(token);
        if (byToken != null){

            return byToken.getEmail();
        }
        return "token is no auth";
    }

}
